#include maps\_utility;
#include common_scripts\utility;

/***********************************************************************
Level
***********************************************************************/

levelVariables()
{
	/*--------------------
	Level Variables
	----------------------*/

	//SetSavedDvar( "compassMaxRange", 1000 );

	level.obj_0 = &"CAMP_ASSAULT_OBJ_0";
	level.obj_1 = &"CAMP_ASSAULT_OBJ_1";
}

flagInits()
{
	/*--------------------
	Flags
	----------------------*/

	// Event 1
	flag_init( "bridgeSceneInPlace" );
	flag_init( "passenger_ready" );
	flag_init( "driver_ready" );

	flag_init( "off_boat_1" );
	flag_init( "off_boat_2" );
	flag_init( "player_unloading" );

	flag_init( "execution_done" );
	
	// Event 2
}

preCacheStuff()
{
	/*--------------------
	PreCache
	----------------------*/

	// Strings
	precacheString( &"CAMPASSAULT_CONTINUED" );

	// Shaders
	precacheShader( "white" );
	precacheShader( "black" );

	// Vehicles
	//maps\_jeep::main( "vehicle_jap_wheeled_type94" );
	maps\_truck::main( "vehicle_jap_wheeled_type94", "model94" );
	//maps\_destructible_type94truck::init();
	build_custom_vehicle( "rubber_raft" );
}

scriptCalls()
{
	/*--------------------
	Script Calls
	----------------------*/

}

threadCalls()
{
	/*--------------------
	Thread Calls
	----------------------*/
}

endMap()
{
	Changelevel( "" );
}

setupRoebuck()
{
	level.Roebuck = getEnt( "roebuck", "script_noteworthy" );
	level.Roebuck.animName = "roebuck";
	level.Roebuck thread magic_bullet_shield();
	level.Roebuck setThreatBiasGroup( "heroes" );
}

setupPolonsky()
{
	level.Polonsky = getEnt( "polonsky", "script_noteworthy" );
	level.Polonsky.animName = "polonsky";
	level.Polonsky thread magic_bullet_shield();
	level.Polonsky setThreatBiasGroup( "heroes" );
}

setupSullivan()
{
	level.Sullivan = getEnt( "sullivan", "script_noteworthy" );
	level.Sullivan.animName = "sullivan";
	level.Sullivan thread magic_bullet_shield();
	level.Sullivan setThreatBiasGroup( "heroes" );
}

setupRedShirt1()
{
	level.redShirt1 = getEnt( "boat1_guy", "targetname" );
	level.redShirt1.animName = "generic";
	level.redShirt1 thread magic_bullet_shield();
	level.redShirt1 setThreatBiasGroup( "support" );
}

setupRedShirt2()
{
	level.redShirt2 = getEnt( "boat2_guy", "targetname" );
	level.redShirt2.animName = "generic";
	level.redShirt2 thread magic_bullet_shield();
	level.redShirt2 setThreatBiasGroup( "support" );
}

/***********************************************************************
Objectives
***********************************************************************/

objectiveAdd( index )
{
	if( index == 0 )
	{
		objective_add( 0, "active", level.obj_0, level.roebuck.origin );
		objective_current( 0 );

		thread followMe( level.roebuck );
	}
	else if( index == 1 )
	{
		objective_state( 0, "done" );
		objective_add( 1, "active", level.obj_1, level.execution.origin );
		objective_current( 1 );
	}

	wait_network_frame();
}

followMe( index )
{
	level endon ( "followMe_End" );

	self endon ( "death" );

	while( 1 )
	{
		Objective_Position( index, self.origin );

		wait( 0.1 );
	}
}

/***********************************************************************
AI HouseKeeping
***********************************************************************/

deleteOnGoal()
{
	self endon ( "death" );

	self waittill ( "goal" );

	self delete();
}

speak( speech, lookAt )
{
	self thread maps\_anim::anim_facialFiller( "dialogue_done", lookAt );
	self animscripts\face::SaySpecificDialogue( undefined, level.scr_sound[ self.animName ][ speech ], 1.0, "dialogue_done" );
	self waittill ( "dialogue_done" );

	// Anything Post Talking
}

/***********************************************************************
Misc
***********************************************************************/

deleteMe( ent, key, value )
{
	if( !isDefined( ent ) )
	{
		println( "deleteMe() called with undefined entity." );
		return;
	}

	if( !isDefined( value ) )
	{
		trigger_wait( key, "targetname" );
	}
	else
	{
		trigger_wait( key, value );
	}

	ent delete();
}

// Borrowed From Mak
build_custom_vehicle( type )
{
	model = undefined;
	death_fx = "explosions/large_vehicle_explosion";
	death_sound = "explo_metal_rand";
	health = 2000;
	min_health = 1000;
	max_health = 5000;
	team = "axis";
	func = undefined;

	if( type == "rubber_raft" )
	{
		model = "makin_raft_rubber";
		death_fx = undefined;
		death_model = "makin_raft_rubber"; // Save Memory Since It Never Gets Destroyed
		health = 7500;
		min_health = 5000;
		max_health = 10000;
		team = "allies";
		func = ::rubber_raft_init;
	}

	maps\_vehicle::build_template( type, model );
	maps\_vehicle::build_life( health, min_health, max_health );
	maps\_vehicle::build_treadfx();
	maps\_vehicle::build_team( team );
	maps\_vehicle::build_localinit( func );

	if( IsDefined( death_fx ) )
	{
		maps\_vehicle::build_deathfx( death_fx, "tag_engine", death_sound, undefined, undefined, undefined, undefined );
	}
}

rubber_raft_init()
{
}

// Borrowed From Mak
custom_introscreen( string1, string2, string3, string4, string5 )
{
	level.introblack = NewHudElem();
	level.introblack.x = 0;
	level.introblack.y = 0;
	level.introblack.horzAlign = "fullscreen";
	level.introblack.vertAlign = "fullscreen";
	level.introblack.foreground = true;
	level.introblack SetShader( "black", 640, 480 );

	flag_wait ( "all_players_connected" );

	freezecontrols_all( true );

	level._introscreen = true;

	wait ( 0.05 );

	level.introstring = [];

	if( IsDefined( string1 ) )
	{
		maps\_introscreen::introscreen_create_line( string1, "lower_left", 1.75 );
	}

	wait ( 2 );

	if( IsDefined( string2 ) )
	{
		maps\_introscreen::introscreen_create_line( string2, "lower_left", 1.75 );
	}

	if( IsDefined( string3 ) )
	{
		maps\_introscreen::introscreen_create_line( string3, "lower_left", 1.75 );
	}

	if( IsDefined( string4 ) )
	{
		wait ( 2 );
		maps\_introscreen::introscreen_create_line( string4, "lower_left", 1.75 );
	}

	if( IsDefined( string5 ) )
	{
		wait ( 2 );
		maps\_introscreen::introscreen_create_line( string5, "lower_left", 1.75 );
	}

	level notify ( "finished final intro screen fadein" );

	wait ( 2 );

	level.introblack FadeOverTime( 0.5 );
	level.introblack.alpha = 0;

	flag_set( "starting final intro screen fadeout" );

	freezecontrols_all( false );

	level._introscreen = false;

	level notify ( "controls_active" );

	level thread maps\_introscreen::introscreen_fadeOutText();

	flag_set( "introscreen_complete" );
}

/***********************************************************************
Network
***********************************************************************/

onPlayerConnect()
{
	for( ;; )
	{
		level waittill ( "connecting", player );

		player thread onPlayerDisconnect();
		player thread onPlayerSpawned();
		player thread onPlayerKilled();

		println( "Player connected to game." );
	}
}

onPlayerDisconnect()
{
	self waittill ( "disconnect" );

	println( "Player disconnected from the game." );
}

onPlayerSpawned()
{
	self endon ( "disconnect" );

	for( ;; )
	{
		self waittill ( "spawned_player" );

		println( "Player spawned in to game at " + self.origin );

		self SetThreatBiasGroup( "players" );
	}
}

onPlayerKilled()
{
	self endon ( "disconnect" );

	for( ;; )
	{
		self waittill ( "killed_player" );

		println( "Player killed at " + self.origin );
	}
}
